﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Entity.Table;
using SqlHelper;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    /// <summary>
    /// 银行流水表数据访问操作
    /// </summary>
    public class PaymentDatabase
    {  
        /// <summary>
        /// 判断是否可以交费
        /// </summary>
        /// <param name="gasUserID"></param>
        /// <returns></returns>
        public static bool IsPayment(string gasUserID,string bankWaterId)
        {
            bool ret = true;
            SqlConnection conn = null;
            SqlParameter[] paramList = new SqlParameter[] { DbHelper.CreateCommandParameter("@UserID", SqlDbType.VarChar, gasUserID),
                                                            DbHelper.CreateCommandParameter("@BankWaterID",SqlDbType.VarChar,bankWaterId),
                                                            DbHelper.CreateCommandOutParameter("@return",SqlDbType.Int)};
            try
            {
                conn = DbHelper.CreateDbConnection(DbHelper.DbConnectionString);
                conn.Open();
                ret = DbHelper.ExecuteNonQueryByProc("NXS_IsPayment", paramList, conn) > 0;
                
            }
            catch (SqlException ex)
            {
                
                ret = false;
            }
            finally
            {
                conn.Close();
            }
            return ret;
        }


        /// <summary>
        /// 添加一条银行流水信息
        /// </summary>
        /// <param name="bw">银行流水实体</param>
        /// <returns></returns>
        public static bool Insert(MPayment bw)
        {
            bool ret = true;
            SqlConnection conn = null;
            DateTime businessDate = Convert.ToDateTime(bw.BusinessDate);
            SqlParameter[] paramList = new SqlParameter[] { DbHelper.CreateCommandParameter("@UserID",SqlDbType.VarChar,bw.GasUserID),
                                                            DbHelper.CreateCommandParameter("@BankCode",SqlDbType.VarChar,bw.BankCode),
                                                            DbHelper.CreateCommandParameter("@BusinessDate",SqlDbType.DateTime,businessDate),
                                                            DbHelper.CreateCommandParameter("@OperatorID",SqlDbType.VarChar,bw.OperatorID),
                                                            DbHelper.CreateCommandParameter("@BankWaterID",SqlDbType.VarChar,bw.BankWaterNumber),
                                                            DbHelper.CreateCommandParameter("@PayMoney",SqlDbType.Decimal,bw.PayMoney),
                                                            DbHelper.CreateCommandParameter("@PayWay",SqlDbType.VarChar,bw.PayWay),
                                                            DbHelper.CreateCommandParameter("@PayWayName",SqlDbType.NVarChar,bw.PayWayName),
                                                            DbHelper.CreateCommandParameter("@CurrentJY",SqlDbType.Decimal,bw.CurrentJY),
                                                            DbHelper.CreateCommandParameter("@Price1",SqlDbType.Decimal,bw.Price1),
                                                            DbHelper.CreateCommandParameter("@Price2",SqlDbType.Decimal,bw.Price2),
                                                            DbHelper.CreateCommandParameter("@Price3",SqlDbType.Decimal,bw.Price3),
                                                            DbHelper.CreateCommandParameter("@BuyGasCount1",SqlDbType.Int,bw.BuyGasCount1),
                                                            DbHelper.CreateCommandParameter("@BuyGasCount2",SqlDbType.Int,bw.BuyGasCount2),
                                                            DbHelper.CreateCommandParameter("@BuyGasCount3",SqlDbType.Int,bw.BuyGasCount3),
                                                            DbHelper.CreateCommandParameter("@GasFee1",SqlDbType.Decimal,bw.GasFee1),
                                                            DbHelper.CreateCommandParameter("@GasFee2",SqlDbType.Decimal,bw.GasFee2),
                                                            DbHelper.CreateCommandParameter("@GasFee3",SqlDbType.Decimal,bw.GasFee3),
                                                            DbHelper.CreateCommandParameter("@BenCiGasCount",SqlDbType.Int,bw.BenCiGasCount),
                                                            DbHelper.CreateCommandParameter("@TotalGasAll",SqlDbType.Int,bw.TotalGasAll),
                                                            DbHelper.CreateCommandOutParameter("@return",SqlDbType.Int)
                                                           };
            try
            {
                conn = DbHelper.CreateDbConnection(DbHelper.DbConnectionString);
                conn.Open();
                ret = DbHelper.ExecuteNonQueryByProc("NXS_Payment_Insert", paramList, conn) > 0;
            }
            catch (SqlException ex)
            {
                ret = false;
            }
            finally
            {
                conn.Close();
            }
            return ret;
        }

        /// <summary>
        /// 判断缴费前后的滞纳金是否一致
        /// </summary>
        /// <param name="gasUserID"></param>
        /// <param name="znj"></param>
        /// <returns></returns>
        public static bool IsEqualZNJ(string gasUserID, double znj)
        {
            bool bRet = true;
            SqlConnection conn = null;
            SqlParameter[] paramList = new SqlParameter[] { DbHelper.CreateCommandParameter("@GasUserID",SqlDbType.VarChar,gasUserID),
                                                            DbHelper.CreateCommandParameter("@ZNJ",SqlDbType.Decimal,znj),
                                                            DbHelper.CreateCommandOutParameter("@return",SqlDbType.Int)};
            try
            {
                conn = DbHelper.CreateDbConnection(DbHelper.DbConnectionString);
                conn.Open();
                bRet = DbHelper.ExecuteNonQueryByProc("CompareZNJ", paramList, conn) > 0;
            }
            catch (SqlException ex)
            {
                bRet = false;
            }
            finally
            {
                conn.Close();
            }
            return bRet;
        }

        /// <summary>
        /// 更新扣款标志为未扣款状态
        /// </summary>
        /// <param name="gasUserID"></param>
        /// <returns></returns>
        public static bool UpdateFlag(string gasUserID)
        {
            bool ret = true;
            SqlConnection conn = null;
            SqlParameter[] paramList = new SqlParameter[] { DbHelper.CreateCommandParameter("@UserID", SqlDbType.VarChar, gasUserID),
                                                            DbHelper.CreateCommandOutParameter("@return",SqlDbType.Int)};
            try
            {
                conn = DbHelper.CreateDbConnection(DbHelper.DbConnectionString);
                conn.Open();
                ret = DbHelper.ExecuteNonQueryByProc("NXS_UpdateKouKuanFlag", paramList, conn) > 0;
            }
            catch (SqlException ex)
            {
                ret = false;
            }
            finally
            {
                conn.Close();
            }
            return ret;
        }
    }
}
